import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-default/index.css';    // 默认主题
// import '../static/css/theme-green/index.css';       // 浅绿色主题
import "babel-polyfill";
import './mock/index.js';
import store from '../vuex/index';
import util from './common/public.js';
import Router from 'vue-router';
import VueResource from 'vue-resource';
import VueHighcharts from "vue2-highcharts";

Vue.use(Router);
Vue.use(VueResource);
Vue.use(ElementUI);
Vue.prototype.$util = util;
Vue.prototype.$axios = axios;

Vue.component("VueHighcharts", VueHighcharts);

Vue.http.options.emulateJSON = true;
Vue.http.options.xhr = {
    withCredentials: true,
    contentType: "application/json;charset=UTF-8",
    datatype: "json"
};
Vue.http.options.headers = {
    rootUnid: store.state.user.rootUnid + ""
};

//vue-resource 拦截器
Vue.http.interceptors.push((request, next) => {

    next((response) => {

        if (response.status == 401) {
            store.dispatch('SIGNOUT');
            location.href = '/#login';
        }

    })

})

//路由跳转前验证是否登录，如果没有则重定向到登录页
router.beforeEach(({ path }, from, next) => {
    let isLogin = Boolean(store.state.user.name)
    if (path == "/502") {
        next();
        return;
    };

    if (!isLogin && path !== '/login') {
        return next({ path: '/login' })
    }
    next();
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app');